﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System.IO;
public class D09_SteamingAssets : MonoBehaviour
{
    private void OnGUI()
    {
        if (GUILayout.Button("读取"))
        {
            StartCoroutine("Read");
        }

        if (GUILayout.Button("读取StreamingAssets的文件,写入persistentDataPath"))
        {
            StartCoroutine("Read1");
        }
    }

    private IEnumerator Read()
    {
        var req = UnityWebRequest.Get(Path.Combine(GetStreamingPath(), "a.txt"));
        yield return req.SendWebRequest();
        Debug.Log(req.downloadHandler.text);
    }

    private IEnumerator Read1()
    {
        var req = UnityWebRequest.Get(Path.Combine(GetStreamingPath(), "a.txt"));
        yield return req.SendWebRequest();
        string writePath = Path.Combine(Application.persistentDataPath, "a.txt");
        File.WriteAllBytes(writePath, req.downloadHandler.data);
        Debug.Log("完成!");
    }

    public static string GetStreamingPath()
    {
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
        return "file://" + Application.dataPath + "/StreamingAssets/";
#elif UNITY_ANDROID
        return "jar:file://" + Application.dataPath + "!/assets/";
#elif UNITY_IPHONE
        return Application.dataPath + "/Raw/";
#else
        return "";
#endif
    }
}